package com.siti.kindnessplatform.base.ctrl;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.siti.kindnessplatform.base.biz.MaterialBiz;
import com.siti.kindnessplatform.base.po.Material;
import com.siti.kindnessplatform.common.CommonResult;
import com.siti.kindnessplatform.util.AESUtil;
import com.siti.kindnessplatform.util.SessionUtil;

@RestController
@RequestMapping("material")
public class MaterialCtrl {

	@Resource
    private MaterialBiz materialBiz;

    @PostMapping(value = "save")
    public CommonResult save(@RequestBody Material material/*,HttpSession session*/) {
//    	String path=SessionUtil.getPath(session);
    	material.setUpdateTime(new Date());
        int flag=materialBiz.save(material/*,path*/);
        if(flag==1){
            return new CommonResult("success","提交成功");
        }else{
            return new CommonResult("error","提交失败");
        }
    }
    
    @PostMapping(value = "saveFiles")
    public CommonResult filesUpload(@RequestParam("files") MultipartFile[] files,HttpSession session) throws IllegalStateException, IOException {  	
    	String path=SessionUtil.getPath(session);
    	try {
    		String picUrls = materialBiz.filesUpload(files,path);
			return new CommonResult("success","上传成功",picUrls);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return new CommonResult("error","上传失败");
    }
    
    @PostMapping(value = "savetest")
    public CommonResult savetest(@RequestBody Material material,HttpSession session) {
    	String path=SessionUtil.getPath(session);
    	material.setUpdateTime(new Date());
        int flag=materialBiz.savetest(material,path);
        if(flag==1){
            return new CommonResult("success","提交成功");
        }else{
            return new CommonResult("error","提交失败");
        }
    }
}
